<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
	String user_name = (session
			.getValue("SPRING_SECURITY_LAST_USERNAME")).toString();
%>
<html>
	<head>
		<title>电压曲线</title>
		<link id=style href="../styles/theme/blue/frame.css" type="text/css"
			rel="stylesheet" />
		<style type="text/css">
.divLinechart {
	width: 100%;
	margin: 5px;
}
</style>
		<script type='text/javascript' src='../ajax-pushlet-client.js'></script>
		<script type='text/javascript' src='../js/jquery.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/jquery.blockUI.js'></script>
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/js/highcharts.js"></script>
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/js/highChartsTheme.js"></script>
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/scripts/curveCommon.js"></script>
		<script>
		function onData(event){
		
			var volData = event.get("volData");
			// 处理A相电压曲线返回数据
		    if(event.get("pnfn") != "" && event.get("pnfn") != undefined && event.get("pnfn") == "0101010b") {
		    	$("#aVolData").val(volData);
		    }
		    else if(event.get("pnfn") != "" && event.get("pnfn") != undefined && event.get("pnfn") == "0101020b") {// 处理B相电压曲线返回数据
		    	$("#bVolData").val(volData);
		    }
		    else if(event.get("pnfn") != "" && event.get("pnfn") != undefined && event.get("pnfn") == "0101040b") {// 处理C相电压曲线返回数据
		    	$("#cVolData").val(volData);
		    }
		    
		    // 如果ABC三相的响应都返回后，就根据返回数据绘制曲线
		    if ($("#aVolData").val() != "" && $("#bVolData").val() != "" && $("#cVolData").val() != "" && $("#aVolData").val() != undefined && $("#bVolData").val() != undefined && $("#cVolData").val() != undefined ) {
			    // 准备A、B、C相电压
			    var aVolDataArr = $("#aVolData").val().split(":");
			    var bVolDataArr = $("#bVolData").val().split(":");
			    var cVolDataArr = $("#cVolData").val().split(":");
			    var startTime = event.get("startTime");
				
				var aVolDataArrDouble = new Array();
				var bVolDataArrDouble = new Array();
				var cVolDataArrDouble = new Array();
				for (var i=0; i<aVolDataArr.length; i++) {
					aVolDataArrDouble[i] = parseFloat(aVolDataArr[i]);
					bVolDataArrDouble[i] = parseFloat(bVolDataArr[i]);
					cVolDataArrDouble[i] = parseFloat(cVolDataArr[i]);
				}
				
				// 修正各曲线的最大值
				var aCorrectYAxisMax = correctYAxisMax(aVolDataArrDouble, 2/3);
				var bCorrectYAxisMax = correctYAxisMax(bVolDataArrDouble, 2/3);
				var cCorrectYAxisMax = correctYAxisMax(cVolDataArrDouble, 2/3);
				
				var maxVal = -999999999;
				maxVal = aCorrectYAxisMax > maxVal ? aCorrectYAxisMax : maxVal;
				maxVal = bCorrectYAxisMax > maxVal ? bCorrectYAxisMax : maxVal;
				maxVal = cCorrectYAxisMax > maxVal ? cCorrectYAxisMax : maxVal;
				maxVal = maxVal > 0.0 ? maxVal : 10;
				
				// 准备时间
				var time = new Array();
				time[0] = startTime;
				var date= new Date(Date.parse(startTime.replace(/-/g,   "/"))); 
				for (var i=1; i<aVolDataArr.length; i++) {
				
					var date2Minsec = date.getTime();
				
					var addFifteen = 1000 * 60 * 15;
					date2Minsec += addFifteen;
					date = new Date(date2Minsec);
					time[i]= date2Str(date);
				}
				
				// 创建折线图，生成后放置在id为container的div中
				titleText = "电压曲线";
				colors = threePhaseColor;
				yAxisText = "电压(V)";
				minVal = 0;
				unit = "V";
				seriesVal = [{
					name: 'A相电压',
					data: aVolDataArrDouble
				}, {
					name: 'B相电压',
					data: bVolDataArrDouble
				},{
					name: 'C相电压',
					data: cVolDataArrDouble
				}];
				var container = parent.window.frames["lineChart"].document.getElementById("container");
				
				showChart(container, titleText, startTime, colors, time, yAxisText, maxVal, minVal, unit, seriesVal);
		    }
		}
		
		// pushlet ajax开始，监听/source/voltage的报文
		PL._init('<%=user_name%>');
		PL.joinListen('/source/voltage');
</script>
	</head>
	<body style="overflow-y: hidden; overflow-x: hidden">
		<input type="hidden" name="aVolData" id="aVolData" />
		<input type="hidden" name="bVolData" id="bVolData" />
		<input type="hidden" name="cVolData" id="cVolData" />
		<div class="divLinechart" id="container">
		</div>
	</body>
</html>
